package com.whimscrew.oauth.config;

import org.apache.commons.lang.RandomStringUtils;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.HashMap;
import java.util.Map;

public class CustomTokenEnhancer implements TokenEnhancer {

    @Override
    public OAuth2AccessToken enhance( OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        Map<String, Object> additionalInfo = new HashMap<>();
        String value =  authentication.getName() + RandomStringUtils.randomAlphabetic(5);
        //自定义增加token属性
        additionalInfo.put( "service",value );
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation( additionalInfo);
        return accessToken;
    }
}